<?php
/**
 * Created by PhpStorm.
 * User: miidi
 * Date: 2018/7/20
 * Time: 9:13
 */

namespace app\api\controller;


use app\api\validate\Idistenger;
use app\api\model\Banner as bannerModel;

class Banner
{
  public function getBannerById($id)
  {
    (new Idistenger())->goCheck();

    //获得该房源的所有banner;
//    $result = House::get($id);
//    return json($result->items);
    //$banner = House::getBannerById($id);

    $banner = bannerModel::where('type_house_id', '=', $id)->select();
    if (!$banner) {
      return json([
          'code' => 0,
          'data' => [],
          'message' => '当前banner不存在！'
      ]);
    }
    $output = [
        'code' => 0,
        'data' => $banner
    ];
    return json($output);
  }

  public function addHouseBanner()
  {

    $data = $_POST['formData'];
    if (!array_key_exists('id', $data)) {
      $house = new bannerModel();
      $data['id'] = null;
      $result = $house->save($data);
      if ($result) {
        return json([
            'code' => 0,
            'message' => '新增成功',
        ]);
      }
    } else {
      $house = bannerModel::get($data['id']);
      $result = $house->save($data);
      if ($result) {
        return json([
            'code' => 0,
            'message' => '更新成功',
        ]);
      }
    }
  }


  public function addHouseMoreBanner()
  {
    $house = new bannerModel();
    $data = $_POST['formData'];
    $result=$house->saveAll($data);
    if ($result) {
      return json([
          'code' => 0,
          'message' => '新增成功',
      ]);
    }
    //return json_encode($data);

//    if (!array_key_exists('id', $data)) {
//      $house = new bannerModel();
//      $data['id'] = null;
//      $result = $house->save($data);
//      if ($result) {
//        return json([
//            'code' => 0,
//            'message' => '新增成功',
//        ]);
//      }
//    } else {
//      $house = bannerModel::get($data['id']);
//      $result = $house->save($data);
//      if ($result) {
//        return json([
//            'code' => 0,
//            'message' => '更新成功',
//        ]);
//      }
//    }
  }

  public function deleteCurrentBanner($id)


  {
    /**
     * User::destroy(1);
     * // 支持批量删除多个数据
     * User::destroy('1,2,3');
     * // 或者
     * User::destroy([1,2,3]);
     */
    $id = input('id');
    $result = bannerModel::destroy($id);
    //return json($result);
    if ($result) {
      return json([
          'code' => 0,
          'messaeg' => 'ok',
          'data' => '删除成功！'
      ]);
    }
  }
}